package com.wn.class30.cotroller.shoppingmall;

//import com.lly835.bestpay.config.WxPayH5Config;
//import com.lly835.bestpay.service.impl.BestPayServiceImpl;
import com.wn.class30.entity.*;
import com.wn.class30.mapper.AllorderDao;
import com.wn.class30.service.ShoppingCarService;
import com.wn.class30.service.ShoppingOrderService;
import com.wn.class30.tools.GetRandomUtils;
import com.wn.class30.tools.Result;
import com.wn.class30.tools.UseridUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ShoppingMallOrder")
@Slf4j
@Api("商城订单")
public class ShoppingOrderController { //地址默认查询显示、地址查询所有、新增删除地址、修改默认地址（前端样式变化，后端先全部清零再指定致1）
    @Resource
    UseridUtils useridUtils;
    @Resource
    ShoppingOrderService shoppingOrderService;

    //根据用户id查询订单中的默认地址
    @GetMapping("/getAddressByUserId")
    @ApiOperation(value = "根据用户id查询订单中的默认地址")
    public Result getAddressByUserId(HttpServletRequest req) throws Exception {
        Integer userid = useridUtils.queryUseridByOptenid(req);
        if(userid == null){
            return  new Result("200","success",null,null);
        }else{
            UserAddress addressByUserId = shoppingOrderService.getAddressByUserId(userid);
            return  new Result("200","success",addressByUserId,null);
        }
    }
    //根据用户id查询订单中的默认地址
    @GetMapping("/deleteUserAddress")
    @ApiOperation(value = "根据用户地址id删除地址")
    public Result deleteUserAddress(HttpServletRequest req,@Valid @NotNull Integer userAddressId) throws Exception {
        Integer userid = useridUtils.queryUseridByOptenid(req);
        if(userid == null){
            return  new Result("200","success",null,null);
        }else{
            shoppingOrderService.deleteUserAddress(userAddressId);
            return  new Result("200","success",null,null);
        }
    }
    //将所有的默认修改为0
    @GetMapping("/updateUserAddressAllDefaultToZero")
    @ApiOperation(value = "将所有的默认修改为0")
    public Result updateUserAddressAllDefaultToZero(HttpServletRequest req) throws Exception {
        Integer userid = useridUtils.queryUseridByOptenid(req);
        if(userid == null){
            return  new Result("200","success",null,null);
        }else{
            shoppingOrderService.updateUserAddressAllDefaultToZero(userid);
            return  new Result("200","success",null,null);
        }
    }
    //修改选中的默认修改为1
    @GetMapping("/updateUserAddressDefaultToOne")
    @ApiOperation(value = "修改选中的默认修改为1")
    public Result updateUserAddressDefaultToOne(HttpServletRequest req,@Valid @NotNull Integer userAddressId) throws Exception {
        Integer userid = useridUtils.queryUseridByOptenid(req);
        if(userid == null){
            return  new Result("200","success",null,null);
        }else{
            shoppingOrderService.updateUserAddressDefaultToOne(userAddressId);
            return  new Result("200","success",null,null);
        }
    }
    //获取购物车的选中商品id数组
    @GetMapping("/getChecks")
    @ApiOperation(value = "获取购物车的选中商品id数组")
    public Result getChecks(HttpServletRequest req,@Valid @NotNull Integer[] checks) throws Exception {
        Integer userid = useridUtils.queryUseridByOptenid(req);
        if(userid == null){
            return  new Result("200","success",null,null);
        }else{
            req.getSession().setAttribute("checks",checks);
            return  new Result("200","success",null,null);
        }
    }
    //获取购物车的选中商品id数组
    @GetMapping("/getChecks2")
    @ApiOperation(value = "获取购物车的选中商品id数组")
    public Result getChecks2(HttpServletRequest req) throws Exception {
        Integer userid = useridUtils.queryUseridByOptenid(req);
        if(userid == null){
            return  new Result("200","success",null,null);
        }else{
            System.out.println(4444);
            Integer[] checks = (Integer[]) req.getSession().getAttribute("checks");
            return  new Result("200","success",checks,null);
        }
    }
    //查询用户成长值比例，展示成长值"
    @GetMapping("/getIntegral")
    @ApiOperation(value = "查询用户成长值比例，展示成长值")
    public Result getIntegral() throws Exception {
        return  new Result("200","success",shoppingOrderService.getIntegral(),null);
    }
    //创建总订单
    @GetMapping("/insertAllOrder")
    @ApiOperation(value = "创建总订单")
    public Result insertAllOrder(HttpServletRequest req,@Valid @NotNull Allorder allorder) throws Exception {
        Integer userid = useridUtils.queryUseridByOptenid(req);
        if(userid == null){
            return  new Result("200","success",null,null);
        }else{
            allorder.setAllCreatetime(new Date());
            allorder.setAllorderNumber(""+new Date().getTime()+"_"+allorder.getUserId()+"_"+GetRandomUtils.GetRandom());
            System.out.println("allorder:::"+allorder);
            shoppingOrderService.insertAllOrder(allorder);
            //创建总订单后获取的其自增id
            Integer getAllorderId = allorder.getAllorderId();
            //将总订单存储到session、便于支付成功后修改子订单状态
            req.getSession().setAttribute("getAllorderId",getAllorderId);
            return  new Result("200","success",getAllorderId,null);
        }
    }
    //创建子订单
    @GetMapping("/insertChildOrder")
    @ApiOperation(value = "创建子订单")
    public Result insertChildOrder(HttpServletRequest req,@Valid @NotNull Myorder myorder) throws Exception {
        Integer userid = useridUtils.queryUseridByOptenid(req);
        if(userid == null){
            return  new Result("200","success",null,null);
        }else{
            System.out.println("子订单："+myorder);
            myorder.setMyorderNumber(""+new Date().getTime()+"_"+myorder.getUserId()+"_"+myorder.getBussinessId()+"_"+GetRandomUtils.GetRandom());
            myorder.setMyorderTime(new Date());
            shoppingOrderService.insertChildOrder(myorder);
            //创建子订单后获取的其自增id
            Integer myorderId = myorder.getMyorderId();
            return  new Result("200","success",myorderId,null);
        }
    }
    //创建订单项
    @GetMapping("/insertOrderDetail")
    @ApiOperation(value = "创建订单项")
    public Result insertOrderDetail(HttpServletRequest req,@Valid @NotNull MyorderDetail myorderDetail) throws Exception {
        Integer userid = useridUtils.queryUseridByOptenid(req);
        if(userid == null){
            return  new Result("200","success",null,null);
        }else{
            System.out.println("订单项订单："+myorderDetail);
            shoppingOrderService.insertOrderDetail(myorderDetail);
            return  new Result("200","success",null,null);
        }
    }

    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    //支付成功后（此为支付宝沙箱测试环境下的支付）
    //存储最终价格数据，支付成功界面获取，后期可以删除此ajax
    @GetMapping("/savetotal_amount")
    @ApiOperation(value = "存储最终价格数据，支付成功界面获取，后期可以删除此ajax")
    public Result savetotal_amount(HttpServletRequest req,@Valid @NotNull String total_amount) throws Exception {
        req.getSession().setAttribute("total_amount",total_amount);
        System.out.println("666666666666666666666666666666666");
        return null;
    }
    @GetMapping("/gettotal_amount")
    @ApiOperation(value = "获取总价格，展示")
    public Result gettotal_amount(HttpServletRequest req) throws Exception {
        String total_amount = (String) req.getSession().getAttribute("total_amount");
        System.out.println("9999999999999999999999999999999999");
        return new Result("200","success",total_amount,null);
    }
    @GetMapping("/updateCuponCount")
    @ApiOperation(value = "获取总价格，展示")
    public Result updateCuponCount(HttpServletRequest req) throws Exception {
        Integer ucId = (Integer) req.getSession().getAttribute("myselect_uc_id");
        //修改或者删除
        if(ucId != null && ucId!=0){

            shoppingOrderService.updateCuponCount(ucId);
        }
        return new Result("200","success",null,null);
    }
    @GetMapping("/updateChildOrderStatus")
    @ApiOperation(value = "获取总价格，展示")
    public Result updateChildOrderStatus(HttpServletRequest req) throws Exception {
        Integer getAllorderId = (Integer) req.getSession().getAttribute("getAllorderId");
        //根据总订单编号修改子订单状态
        shoppingOrderService.updateChildOrderStatus(getAllorderId);
        return new Result("200","success",null,null);
    }
    @GetMapping("/updateUserPoints")
    @ApiOperation(value = "获取总价格，展示")
    public Result updateUserPoints(HttpServletRequest req) throws Exception {
        Integer userid = useridUtils.queryUseridByOptenid(req);
        Integer getAllorderId = (Integer) req.getSession().getAttribute("getAllorderId");
        if(userid == null){
            return  new Result("200","success",null,null);
        }else{
            //查询订单项积分和、增加用户积分。
            shoppingOrderService.updateUserPoints(userid,getAllorderId);
            return  new Result("200","success",null,null);
        }
    }
    @GetMapping("/deleteCarInOrder")
    @ApiOperation(value = "删除购物车商品")
    public Result deleteCarInOrder(HttpServletRequest req) throws Exception {
        Integer userid = useridUtils.queryUseridByOptenid(req);
        Integer[] priduct_ids = (Integer[]) req.getSession().getAttribute("checks");
        if(userid == null){
            return  new Result("200","success",null,null);
        }else{
            shoppingOrderService.deleteCarInOrder(userid,priduct_ids);
            return  new Result("200","success",null,null);
        }
    }







}
